package com.fly.caihong.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.fly.caihong.R;
import com.fly.caihong.utils.StatusBarUtils;
import com.fly.caihong.view.SimpleToolbar;
import com.fly.caihong.view.loading.FLoadingDialog;


public abstract class BaseActivity extends AppCompatActivity {

    protected SimpleToolbar mToolbar;
    protected FLoadingDialog mLoadingDialog;
    private boolean isLightMode;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        setStatusBarStyle();
        initViews();
        initListeners();
        initData();
    }

    protected void setStatusBarStyle() {
        StatusBarUtils.setLightMode(this);
        isLightMode = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    protected void initViews() {
        mToolbar = findViewById(R.id.tb_toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            mToolbar.setLeftBackResource(isLightMode ? R.drawable.ic_back_gray
                    : R.drawable.ic_back_white);
            mToolbar.setOnLeftClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    /**
     * 显示加载进度条
     */
    protected void showLoading() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new FLoadingDialog(this);
        }
        mLoadingDialog.show();
    }

    /**
     * 显示加载进度条
     *
     * @param msg
     */
    protected void showLoading(String msg) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new FLoadingDialog(this);
        }
        mLoadingDialog.show(msg);
    }

    /**
     * 显示加载进度条
     *
     * @param resId
     */
    protected void showLoading(@StringRes int resId) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new FLoadingDialog(this);
        }
        mLoadingDialog.show(getString(resId));
    }

    /**
     * 替换显示内容
     * @param msg
     */
    protected void replaceLoading(String msg) {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.setLoadingMsg(msg);
        }
    }

    /**
     * 移除加载进度条
     */
    protected void dismissLoading() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    /**
     * 初始化监听器
     */
    protected void initListeners() {
    }

    /**
     * 初始化数据
     */
    protected void initData() {
    }

    /**
     * 内容布局xml
     *
     * @return
     */
    @LayoutRes
    protected abstract int getLayout();
}
